package javachess;

public class Queen extends Piece
{
	public Queen(int a_owner, int a_x, int a_y)
	{
		super ('q', a_owner);
		m_owner = a_owner;
		m_x = a_x;
		m_y = a_y;
	}
	/**
	 * -Takes in an x and y destination and determines if the location is acceptable.
	 * -Returns a boolean to indicate acceptance.
	 * @param a_x
	 * @param a_y
	 * @return
	 */
	boolean validateMove(Player player, Player otherPlayer, int a_x, int a_y)
	{
		int selectedSquareX = a_x;
		int selectedSquareY = a_y;
		int SSX = a_x; // SS is the starting, plain x and y are the target
		int SSY = a_y;
		
		if(
		(a_x != m_x && a_y != m_y)
		||
		(!(((SSX - SSY) == (m_x - m_y))
				||
				((SSX + SSY) == (m_x + m_y))))
		)
			return false;
		if(a_y == m_y)								// destination has the same y
		{
			if(selectedSquareX < m_x)									// destination is to the right
			{
				for(int mid = selectedSquareX; mid < m_x; ++mid)			// make a variable that moves from left start to right finish
					if(mid != selectedSquareX)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == mid
							&& player.m_pieces.get(i).m_y == a_y)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y)
							{
								return false;
							}
						}
					}
			}
			else													// destination is to the left
			{
				for(int mid = selectedSquareX; mid > m_x; --mid)			// make a variable that moves from right start to left finish
					if(mid != selectedSquareX)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == mid
							&& player.m_pieces.get(i).m_y == a_y)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y)
							{
								return false;
							}
						}
					}
			}
		}
		else if(a_x == m_x)													// destination has the same x
		{
			if(selectedSquareY < m_y)									// destination is up
			{
				for(int mid = selectedSquareY; mid < m_y; ++mid)			// make a variable that moves from lower start to upper finish
					if(mid != selectedSquareY)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x
							&& player.m_pieces.get(i).m_y == mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x
							&& otherPlayer.m_pieces.get(i).m_y == mid)
							{
								return false;
							}
						}
					}
			}
			else													// destination is down
			{
				for(int mid = selectedSquareY; mid > m_y; --mid)			// make a variable that moves from upper start to lower finish
					if(mid != selectedSquareY)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x
							&& player.m_pieces.get(i).m_y == mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x
							&& otherPlayer.m_pieces.get(i).m_y == mid)
							{
								return false;
							}
						}
					}
			}
		}
		else if(SSY < m_y)								// destination is down
		{
			if(SSX < m_x)									// destination is to the right
			{
				for(int mid = 0; mid < m_x - SSX; ++mid)			// make a variable that moves from right start to left finish
					if(mid != 0)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x + mid
							&& player.m_pieces.get(i).m_y == a_y + mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x + mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y + mid)
							{
								return false;
							}
						}
					}
			}
			else													// destination is to the left
			{
				for(int mid = 0; mid < m_y - SSY; ++mid)			// make a variable that moves from left start to right finish
					if(mid != 0)					
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x + mid
							&& player.m_pieces.get(i).m_y == a_y - mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x + mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y - mid)
							{
								return false;
							}
						}
					}
			}
		}
		else													// destination is up
		{
			if(SSX < m_x)									// destination is to the right
			{
				for(int mid = 0; mid < SSY - m_y; ++mid)			// make a variable that moves from right start to left finish
					if(mid != 0)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x - mid
							&& player.m_pieces.get(i).m_y == a_y + mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x - mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y + mid)
							{
								return false;
							}
						}
					}
			}
			else													// destination is to the left
			{
				for(int mid = 0; mid < SSX - m_x; ++mid)			// make a variable that moves from right start to left finish
					if(mid != 0)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x - mid
							&& player.m_pieces.get(i).m_y == a_y - mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x - mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y - mid)
							{
								return false;
							}
						}
					}
			}
		}

		return true;												// ALL SYSTEMS GO!
	}
}
